using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GoodStuff;

namespace Octopus111
{
    public partial class FormCaptureControl : Form
    {
        Graphics _moviePreview;
        public FormCaptureControl()
        {
            InitializeComponent();
            _moviePreview = groupBoxCapturePreview.CreateGraphics();
            folderBrowserDialogChooseMovieFolder.SelectedPath = 
                System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"\\Octopus Movies";
        }

        private void buttonCaptureStart_Click(object sender, EventArgs e)
        {
            progressBarCaptureProgress.Increment(1);
        }

        private void buttonChooseFolder_Click(object sender, EventArgs e)
        {
            folderBrowserDialogChooseMovieFolder.ShowDialog(this);
        }

         public void SetStateVariables() 
         {
             StateStore s = StateStore.GetStore();

            // validate the range of the variables before saving

             // region size must be even
             numericUpDownCaptureRegionSize.Value -= numericUpDownCaptureRegionSize.Value % 2;

             // make sure the capture region does not extend off the buffer
             numericUpDownXCenter.Minimum = numericUpDownCaptureRegionSize.Value / 2;
             numericUpDownYCenter.Minimum = numericUpDownCaptureRegionSize.Value / 2;
             Rectangle fbs = (Rectangle)s["FrameBufferSize"];
             numericUpDownXCenter.Maximum = fbs.Width - numericUpDownCaptureRegionSize.Value / 2;
             numericUpDownYCenter.Maximum = fbs.Height - numericUpDownCaptureRegionSize.Value / 2;
             Decimal MaxSize = Math.Min( numericUpDownXCenter.Value, numericUpDownYCenter.Value );
             MaxSize = Math.Min(MaxSize, fbs.Right - numericUpDownXCenter.Value);
             MaxSize = Math.Min(MaxSize, fbs.Bottom - numericUpDownYCenter.Value);
             numericUpDownCaptureRegionSize.Maximum = 2 * MaxSize;

             // capture region
             Rectangle r = new 
                 Rectangle( 
                 (int)numericUpDownXCenter.Value - (int)numericUpDownCaptureRegionSize.Value / 2, 
                 (int)numericUpDownYCenter.Value - (int)numericUpDownCaptureRegionSize.Value / 2, 
                 (int)numericUpDownCaptureRegionSize.Value, 
                 (int)numericUpDownCaptureRegionSize.Value );
             s.SetVariable("CaptureRegion",r);

             // movie parameters
             s.SetVariable("OutputPath",folderBrowserDialogChooseMovieFolder.SelectedPath);
             s.SetVariable("MovieName",textBoxMovieName.Text);
             s.SetVariable("CaptureEveryNthFrame", new Decimal((int)numericUpDownCaptureEveryNth.Value));
             s.SetVariable("CaptureNFrames", new Decimal((int)numericUpDownCaptureNFrames.Value));
        	
         }
        public void PassthroughClick(object sender, MouseEventArgs m)
        {
            StateStore s = StateStore.GetStore();
            Rectangle r = (Rectangle)s["FrameBufferSize"];
            RawFrameProcessor rfp = s["RawFrameProcessor"] as RawFrameProcessor;
            Decimal x = m.X * r.Width / rfp.PassthruArea.Width;
            Decimal y = m.Y * r.Height / rfp.PassthruArea.Height;
	        // validate the range of the argumetns
            x = Math.Max(numericUpDownXCenter.Minimum, x);
            y = Math.Max(numericUpDownYCenter.Minimum, y);
            x = Math.Min(numericUpDownXCenter.Maximum, x);
            y = Math.Min(numericUpDownYCenter.Maximum, y);

	        // set the new center to the location of the click
            numericUpDownXCenter.Value = x;
            numericUpDownYCenter.Value = y;
	        SetStateVariables();
        }
        private void numericUpDown_ValueChanged(Object  sender, EventArgs  e) 
        {
		    SetStateVariables();
	    }

        private void FormCaptureControl_FormClosing(object sender, FormClosingEventArgs e)
        {
            Housekeeper.Cleanup();
        }
        public void SetBitmap(Bitmap b)
        {
            _moviePreview.DrawImage(
                b, 
                new Rectangle(
                    3, 
                    10, 
                    groupBoxCapturePreview.Width - 6,
                    groupBoxCapturePreview.Height - 13)
            );
        }
    }
}